/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.ban.ycl.wx.audio.yzserver.utils;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import org.apache.http.HttpResponse;

/**
 *
 * @author zhangyun
 */
public class HttpResponseParser {


    public static String getResponseContent(HttpResponse response) {

        InputStream reponseStream = null;
        try {
            reponseStream = response.getEntity().getContent();
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            byte[] bytesContent = new byte[1024];
            int bytesReadIn = 0;

            while ((bytesReadIn = reponseStream.read(bytesContent, 0, 1024)) > 0) {
                out.write(bytesContent, 0, bytesReadIn);
            }
            out.close();
            byte[] rep = out.toByteArray();

            return new String(rep, "UTF-8");
        } catch (Exception ex) {
            return null;
        } finally {
            try {
                reponseStream.close();
            } catch (Exception ex) {
            }
        }
    }

    public static String getResult(HttpResponse response) {
        String result = "";
        InputStream reponseStream = null;
        try {
            reponseStream = response.getEntity().getContent();
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            byte[] bytesContent = new byte[1024];
            int bytesReadIn = 0;

            while ((bytesReadIn = reponseStream.read(bytesContent, 0, 1024)) > 0) {
                out.write(bytesContent, 0, bytesReadIn);
            }
            out.close();
            byte[] rep = out.toByteArray();

            String responseStr = new String(rep, "UTF-8");
            if (responseStr.toString().contains("1")) {
                result = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"
                        + "<response>"
                        + "<status>client has start success!</status>"
                        + "</response>";

            } else if (responseStr.toString().contains("0")) {
                result = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"
                        + "<error>"
                        + "<message>client start err!</message>"
                        + "</error>";
            } else {
                result = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"
                        + "<error>"
                        + "<message>client start err!</message>"
                        + "</error>";
            }

        } catch (Exception ex) {
            result = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"
                    + "<error>"
                    + "<message>client start err!</message>"
                    + "</error>";
        }

        return result;
    }
}
